package cn.wolfcode.trip.base.domain;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Setter
@Getter
/**
 * 日报
 */
public class Daily extends BaseDomain {
    //id主键
    //标题
    private String title;
    //封面
    private String coverUrl;
    //状态,是否发布
    private Integer state;
    //排序,是否推荐,注意只可以有一条日报可以被推荐
    private Integer sequence;
    //创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;
    //发布时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date releaseTime;
    //浏览量
    private Integer browseNum;
    //日报内容
    private DailyContent dailyContent;

    //点赞数(不用出现在数据库字段中)
    private Integer likeNum;

    //是否已经点赞(不用出现在数据库字段中)
    private boolean like;

    //评论数
    private int commentNum;


    //用于模态框的回显
    public String getJson() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("title", title);
        map.put("coverUrl", coverUrl);
        map.put("sequence", sequence);
        map.put("state", state);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String createTimeFormat = sdf.format(createTime);
        map.put("createTime", createTimeFormat);
        if (releaseTime != null) {
            String releaseTimeFormat = sdf.format(releaseTime);
            map.put("releaseTime", releaseTimeFormat);
        }
        return JSON.toJSONString(map);
    }

    public String getStateName() {
        return state == 0 ? "未发布" : "已发布";
    }

    public String getSequenceName() {
        return sequence == 0 ? "未推荐" : "已推荐";
    }

}